package com.test.ddd.infrastructure.eventbus;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @author csruan
 * @Copyright
 * @Description
 * @date 2021/9/9 14:04
 */
@Component
public class EventSubscribeBeanPostProcessor implements BeanPostProcessor {
	@Autowired
	EventBus eventBus;

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		Method[] methods = bean.getClass().getMethods();
		for (Method method : methods) {
			Annotation[] annotations = method.getAnnotations();
			for (Annotation annotation : annotations) {
				if (annotation.annotationType().equals(Subscribe.class)) {
					// 如果这是一个Guava @Subscribe注解的事件监听器方法，说明所在bean实例
					// 对应一个Guava事件监听器类，将该bean实例注册到Guava事件总线
					eventBus.register(bean);
					return bean;
				}
			}
		}
		return bean;
	}
}
